package com.kim.rssreader.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;

public class ConfigUtils {

	/**
	 * 数据配置文件
	 */ 
	public static final String DBConfig = "dbconfig.xml";

	/**
	 * 根据keys获取value并封装到Bundle
	 * 
	 * @param context
	 * @param sharedPreferencesName
	 * @param bundle
	 * @param keys
	 */
	public static void loadConfigIntoBundle(Context context,
			String sharedPreferencesName, Bundle bundle, String[] keys) {
		if (keys == null) {
			return;
		}
		if (sharedPreferencesName == null || "".equals(sharedPreferencesName)) {
			return;
		}

		SharedPreferences preferences = getSharedPreferencesPrivate(context,
				sharedPreferencesName);
		for (String key : keys) {
			bundle.putString(key, preferences.getString(key, null));
		}
		preferences = null;
		keys = null;
		sharedPreferencesName = null;
	}

	/**
	 * 根据Key获取值
	 * 
	 * @param context
	 * @param sharedPreferencesName
	 * @param key
	 * @return
	 */
	public static String getValueForKey(Context context,
			String sharedPreferencesName, String key) {
		SharedPreferences preferences = getSharedPreferencesPrivate(context,
				sharedPreferencesName);
		if (preferences == null) {
			return null;
		}

		return preferences.getString(key, null);
	}

	/**
	 * 获取sharedPreference
	 * 
	 * @param context
	 * @param sharedPreferencesName
	 * @return
	 */
	public static SharedPreferences getSharedPreferencesPrivate(
			Context context, String sharedPreferencesName) {
		return context.getSharedPreferences(sharedPreferencesName,
				Context.MODE_PRIVATE);
	}

	/**
	 * 获取sharedPreference
	 * 
	 * @param context
	 * @param sharedPreferencesName
	 * @param mode
	 * @return
	 */
	public static SharedPreferences getSharedPreferences(Context context,
			String sharedPreferencesName, int mode) {
		return context.getSharedPreferences(sharedPreferencesName, mode);
	}

}
